<?php

namespace App\Repository;

use App\Consts\GlobalConst;
use App\Consts\GlobalNoteConst;
use App\Models\IndexNote;
use App\Models\UserMessageLikeReceive;
use App\Models\UserMessageLikeSend;

/**
 * @package App\Repository
 * @see LikeRepository
 */
class LikeRepository extends BaseRepository
{
    public function getUserLikeList($searchParams)
    {
        $likeTableName = UserMessageLikeSend::getTableNameById($searchParams['sender_id']);
        $provider = UserMessageLikeSend::getInstance($searchParams['sender_id'])
            ->where($likeTableName . '.sender_id', $searchParams['sender_id'])
            ->where($likeTableName . '.target_type', GlobalConst::TARGET_TYPE_NOTE)
            ->where($likeTableName . '.is_deleted', GlobalConst::IS_NOT_DELETE);

        $isPublic = [GlobalNoteConst::IS_PUBLIC];
        if ($searchParams['sender_id'] === $searchParams['current_user_id']) {
            $isPublic = [GlobalNoteConst::IS_PUBLIC, GlobalNoteConst::IS_NOT_PUBLIC];
        }
        $provider->leftJoin(IndexNote::tableName(), static function ($join) use ($likeTableName) {
            $join->on($likeTableName . '.target_id', '=', IndexNote::tableName() . '.note_id');
        });

        $provider->whereIn(IndexNote::tableName() . '.is_public', $isPublic)
            ->where(IndexNote::tableName() . '.is_deleted', GlobalConst::IS_NOT_DELETE)
            ->whereIn(IndexNote::tableName() . '.type', [GlobalNoteConst::TYPE_ARTICLE_NOTE, GlobalNoteConst::TYPE_LINE_NOTE]);

        if (!empty($searchParams['last_id'])) {
            $model = UserMessageLikeSend::getInstance($searchParams['sender_id'])
                ->where('message_id', $searchParams['last_id'])
                ->first();
            if ($model) {
                $provider->where($likeTableName . '.id', '<', $model->id);
            }
        }

        $provider->orWhere(static function ($query) use ($searchParams, $likeTableName) {
            $query->where($likeTableName . '.sender_id', $searchParams['sender_id'])
                ->where($likeTableName . '.is_deleted', GlobalConst::IS_NOT_DELETE)
                ->where($likeTableName . '.target_type', GlobalConst::TARGET_TYPE_DYNAMIC);
            if (!empty($searchParams['last_id'])) {
                $model = UserMessageLikeSend::getInstance($searchParams['sender_id'])
                    ->where('message_id', $searchParams['last_id'])
                    ->first();
                if ($model) {
                    $query->where($likeTableName . '.id', '<', $model->id);
                }
            }
        });

        $provider->select($likeTableName . '.*');

        $provider->orderByDesc($likeTableName . '.updated_at');
        $provider->orderByDesc($likeTableName . '.id');

        return $this->setPagination($provider);
    }

    /**
     *
     * 新增【我发出的点赞】记录
     * @param $loadData
     * @return UserMessageLikeSend|\Illuminate\Database\Eloquent\Builder|null
     */
    public function createLikeSend($loadData)
    {
        return UserMessageLikeSend::getModel($loadData['sender_id'])->create($loadData);
    }

    /**
     * 新增【我收到的点赞】记录
     * @param $loadDat
     * @return UserMessageLikeReceive|\Illuminate\Database\Eloquent\Builder|null
     */
    public function createLikeReceive($loadData)
    {
        return UserMessageLikeReceive::getModel($loadData['receive_id'])->create($loadData);
    }

    /**
     * 修改【我发出的点赞】记录
     * @param UserMessageLikeSend $userMessageLikeSend
     * @param $loadData
     * @return int|bool
     */
    public function updateLikeSend(UserMessageLikeSend $userMessageLikeSend, $loadData)
    {
        return $userMessageLikeSend->update($loadData);
    }

    /**
     * 修改【我收到的点赞】记录
     * @param UserMessageLikeReceive $userMessageLikeReceive
     * @param $loadData
     * @return bool|int
     */
    public function updateLikeReceive(UserMessageLikeReceive $userMessageLikeReceive, $loadData)
    {
        return $userMessageLikeReceive->update($loadData);
    }

    /**
     * 通过【主体类型】获取用户点赞列表
     * @param $userId
     * @param $targetType
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getLikeListByTargetType($userId, $targetType)
    {
        return UserMessageLikeSend::getInstance($userId)
            ->select(['target_id', 'is_deleted'])
            ->where('sender_id', $userId)
            ->where('target_type', $targetType)
            ->get();
    }

    /**
     * 获取否点赞过某主体记录
     * @param $senderId
     * @param $targetId
     * @return \Illuminate\Database\Eloquent\Builder|UserMessageLikeSend|object|null
     */
    public function getLikeSendBySenderIdAndTargetId($senderId, $targetId)
    {
        return UserMessageLikeSend::getInstance($senderId)
            ->where('sender_id', $senderId)
            ->where('target_id', $targetId)
            ->first();
    }

    /**
     * 获取【我收到的点赞】记录
     * @param $receiveId
     * @param $targetId
     * @return \Illuminate\Database\Eloquent\Builder|UserMessageLikeReceive|object|null
     */
    public function getLikeReceiveByReceiveIddAndTargetId($receiveId, $targetId)
    {
        return UserMessageLikeReceive::getInstance($receiveId)
            ->where('receive_id', $receiveId)
            ->where('target_id', $targetId)
            ->first();
    }
}
